package com.bdqn.ch02.JD.jd4;


import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.net.MalformedURLException;
import java.util.Iterator;

/**
 * DOM4JSelect
 *
 * 使用 DOM4J 进行对文档的查询
 * @aurhor Administrator  whs
 * @since 2024/7/1
 */
public class DOM4JSelect {

    public static void main(String[] args) {
        Document doc = null;
        try {
            SAXReader saxReader = new SAXReader();
            doc = saxReader.read(new File("src/com/bdqn/ch02/JD/成绩信息.xml"));
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        }
        Element root = doc.getRootElement();
        // 循环id
        for(Iterator itstudent = root.elementIterator(); itstudent.hasNext();){
            Element studentEle = (Element) itstudent.next();
            System.out.println("id："+studentEle.attributeValue("id"));
            // 循环成绩
            for (Iterator itType = studentEle.elementIterator();itType.hasNext();){
                Element typeEle = (Element) itType.next();
                System.out.println("成绩："+typeEle.getText());
            }
            System.out.println();
        }
    }
    /**
     * id：2
     * 成绩：阿贡
     * 成绩：110
     * 成绩：60
     *
     * id：4
     * 成绩：听封
     * 成绩：60
     * 成绩：英语
     *
     * id：3
     * 成绩：帅帅
     * 成绩：100
     * 成绩：数学
     */
}
